package com.yingxin.yxerp.module.appversion.controller;

import com.yingxin.yx.framework.commons.constant.PathConst;
import com.yingxin.yx.framework.commons.exception.BizException;
import com.yingxin.yx.framework.commons.helper.PropertiesHelper;
import com.yingxin.yx.framework.user.core.IUserSupport;
import com.yingxin.yxerp.module.appversion.entity.AppVersion;
import com.yingxin.yxerp.module.appversion.mapper.AppVersionMapper;
import com.yingxin.yxerp.module.appversion.service.IAppVersionService;
import com.yingxin.yxerp.module.appversion.vo.req.AppVersionCreateVo;
import com.yingxin.yxerp.module.appversion.vo.req.AppVersionQueryVo;
import com.yingxin.yxerp.module.appversion.vo.resp.AppVersionRespVo;
import com.yingxin.yxerp.module.attachment.service.IAttachmentService;
import com.yingxin.yxerp.module.easypoi.util.WebFilenameUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/appinfo")
public class AppVersionController {

    @Autowired
    private AppVersionMapper appVersionMapper;

    @Autowired
    private IAppVersionService appVersionService;

    @Autowired
    private IUserSupport support;

    @Autowired
    private IAttachmentService attachmentService;

    @ApiOperation(value = "获取版本")
    @GetMapping("/version")
    public AppVersionRespVo version() {
        AppVersionQueryVo queryVo = new AppVersionQueryVo();
        return appVersionMapper.getVersionTopOne(queryVo);
    }

    @PostMapping("/list")
    public List<AppVersionRespVo> listVo(@RequestBody AppVersionQueryVo queryVo){
        return appVersionMapper.listVo(queryVo);
    }

    @PostMapping("/addOrUp")
    public void create(@RequestBody AppVersionCreateVo queryVo){
        AppVersion appVersion = new AppVersion();
        BeanUtils.copyProperties(queryVo,appVersion);
        if (queryVo.getId() != null){
            appVersion.setVersionId(support.getUserId());
            appVersion.setVersionTime(new Date());
            appVersionService.updateById(appVersion);

        }else {
            appVersion.setVersionId(support.getUserId());
            appVersion.setVersionTime(new Date());
            appVersionService.save(appVersion);
        }

    }

    @GetMapping("/getVo/{id}")
    public AppVersionRespVo getVo(@PathVariable("id")String id){
        return appVersionMapper.getVo(id);
    }

    @PostMapping("/remove/{id}")
    public void remove(@PathVariable("id")String id){
        appVersionService.removeById(id);
    }


    @ApiOperation(value = "获取app下载地址")
    @PostMapping("/downloadApp")
    public String downloadApp() {
        String path = PropertiesHelper.getInstance().getProperty(PathConst.EXPORT_APP)+"/xintai-erp-app.apk";
        File file = new File(path);
        if (!file.exists()) {
            throw new BizException("没有文件！");
        }
        return WebFilenameUtils.encode("xintai-erp-app.apk");
    }

}
